package com.hz.friends.activity;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVUser;
import com.avos.avoscloud.RequestPasswordResetCallback;
/**
 * 找回密码
 * 
 *  last edit 2014/7/11 13:46
 * @author shz
 * 
 */
public class FindBackMPasswordActivity extends BaseActivity {

	private EditText edtx_mail;
	private Button btn_sendmail;
	private final String  TAG ="FindBackMPasswordActivity";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_findbackmpassword);
		initactionbar();
		setView();
	}
	private void initactionbar() {
		ImageButton imbtn_bar_left = (ImageButton) findViewById(R.id.imbtn_bar_left);
		ImageButton imbtn_bar_right = (ImageButton) findViewById(R.id.imbtn_bar_right);
		TextView imbtn_bar_title = (TextView) findViewById(R.id.imbtn_bar_title);
		imbtn_bar_right.setVisibility(View.INVISIBLE);
		imbtn_bar_left.setImageResource(R.drawable.ic_leftback_arrow);
		imbtn_bar_title.setText("找回密码");
		imbtn_bar_left.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
               finish();				
			}
		});
	}

	private void setView() {
		edtx_mail = (EditText) findViewById(R.id.edtx_mail);
		btn_sendmail = (Button) findViewById(R.id.btn_sendmail);
		btn_sendmail.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				String mail = edtx_mail.getText().toString().trim();
				if (mail.length() == 0) {
					showToast("请输入您的绑定邮箱");
					return;
				}
				if (!checkEmail(mail)) {
					showToast("邮箱输入错误");
					return;
				}
				createWiatDialog("正在发送");
				//发送email
				AVUser.requestPasswordResetInBackground(mail,
						new RequestPasswordResetCallback() {
							public void done(AVException e) {
								Log.d(TAG, "MyTag:requestPasswordResetInBackground e:"+e);
								cancleWaitDialog();
								if (e == null) {
									// 已发送一份重置密码的指令到用户的邮箱
									showToast("已发送一份重置密码邮件到您的邮箱，请注意查收");
									FindBackMPasswordActivity.this.finish();
								} else {
									// 重置密码出错。
									showToast("此邮箱未绑定，请重新填写");
								}
							}
						});

			}
		});
	}

	/**
	 * 验证邮箱地址是否正确
	 * 
	 * @param email
	 * @return
	 */
	public boolean checkEmail(String email) {
		boolean flag = false;
		try {
			String check = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
			Pattern regex = Pattern.compile(check);
			Matcher matcher = regex.matcher(email);
			flag = matcher.matches();
		} catch (Exception e) {
			e.printStackTrace();
			Log.e(TAG, "MyTag:验证邮箱地址错误");
			flag = false;
		}
		return flag;
	}
	@Override
	protected void init() {
		// TODO Auto-generated method stub
		
	}
	@Override
	protected void initViews() {
		// TODO Auto-generated method stub
		
	}
	@Override
	protected void setViews() {
		// TODO Auto-generated method stub
		
	}
	@Override
	protected void initEvents() {
		// TODO Auto-generated method stub
		
	}

}
